﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace geetol.sms.core
{
    public class Logs
    {
        private string PathName;
        private string FileName;

        /// <summary>
        /// 构造 Log
        /// </summary>
        /// <param name="pathname">相对于网站根目录 App_Log 目录的相对路径，如： System， 就相当于 ~/App_Log/System/</param>
        public Logs(string pathname)
        {
            if (String.IsNullOrEmpty(pathname))
            {
                throw new Exception("没有初始化 Log 类的 PathName 变量");
            }

            PathName = System.AppDomain.CurrentDomain.BaseDirectory + "log\\" + pathname;

            ini(pathname);
        }

        public void ini(string pathname)
        {


            if (!Directory.Exists(PathName))
            {
                try
                {
                    Directory.CreateDirectory(PathName);
                }
                catch { }
            }

            if (!Directory.Exists(PathName))
            {
                PathName = System.AppDomain.CurrentDomain.BaseDirectory + "App_Log";

                if (!Directory.Exists(PathName))
                {
                    try
                    {
                        Directory.CreateDirectory(PathName);
                    }
                    catch { }
                }

                if (!Directory.Exists(PathName))
                {
                    PathName = System.AppDomain.CurrentDomain.BaseDirectory;
                }
            }

            FileName = PathName + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
        }

        public void Write(Exception ex)
        {
            ini(PathName);
            Write(ex.Message + " ---- " + ex.StackTrace);
        }

        public void Write(string Message)
        {
            ini(PathName);
            if (String.IsNullOrEmpty(FileName))
            {
                return;
            }
            try
            {
                using (FileStream fs = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.Write))
                {
                    StreamWriter writer = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GBK"));

                    try
                    {
                        writer.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + System.DateTime.Now.Millisecond.ToString() + "\t\t" + Message + "\r\n");
                    }
                    catch { }

                    writer.Close();
                }
            }
            catch { }
        }

        public static void run(string className, string content)
        {
            new Logs("run").Write(className + " " + content);
        }

        public static void Info(string className, string content)
        {
            new Logs("info").Write(className + " " + content);
        }

        public static void Error(Exception ex)
        {
            new Logs("Error").Write(ex.Source + "," + ex.TargetSite + "," + ex.Message + "," + ex.StackTrace);
        }

        public static void Error(string className, string content)
        {
            new Logs("Error").Write(className + " " + content);
        }

        public static void Debug(string className, string content)
        {
            new Logs("Debug").Write(className + " " + content);
        }

        public static void Result(string className, string content)
        {
            new Logs("Result").Write(className + " " + content);
        }
    }
}
